package com.suoluo.zenapp.common;

public final class PageUtil {

	private PageUtil() {

	}

	/**
	 * 计算分页的起始行
	 * 
	 * @param curPage
	 *            当前
	 * @param pageRows
	 *            每页行记录数
	 * @return 返回当前页的起始行
	 */
	public static int calcStartRow(int curPage, int pageRows) {
		pageRows = pageRows == 0 ? 10 : pageRows;
		curPage = curPage == 0 ? 1 : curPage;
		int start = (curPage - 1) * pageRows;
		return start;
	}

	/**
	 * 计算总共的页数
	 * 
	 * @param totalCount
	 *            总的记录条数
	 * @param perPageCount
	 *            每页的记录数
	 * @return
	 */
	public static int calcPageCount(int totalCount, int perPageCount) {
		return (totalCount + perPageCount - 1) / perPageCount;
	}

	public static PageIndexBean calcPageIndexRange(int curPage, int totalCount,
			int pageSize) {

		int totalPages = (totalCount + pageSize - 1) / pageSize;
		int startPage = curPage - 3;
		int endPage = curPage + 5;
		if (startPage < 1) {
			int preRemain = -(startPage - 1);
			startPage = 1;
			endPage += preRemain;
		}
		if (endPage > totalPages) {
			int traillingRemain = endPage - totalPages;
			endPage = totalPages;
			startPage -= traillingRemain;
			if (startPage < 0) {
				startPage = 1;
			}
		}

		PageIndexBean pageIndexBean = new PageIndexBean();
		pageIndexBean.setCurPage(curPage);
		pageIndexBean.setStartPage(startPage);
		pageIndexBean.setEndPage(endPage);
		return pageIndexBean;
	}

	/**
	 * 分页索引
	 * 
	 * @author xiongxu
	 * 
	 */
	public static class PageIndexBean {
		private int startPage;

		private int endPage;

		private int curPage;

		public int getStartPage() {
			return startPage;
		}

		public void setStartPage(int startPage) {
			this.startPage = startPage;
		}

		public int getEndPage() {
			return endPage;
		}

		public void setEndPage(int endPage) {
			this.endPage = endPage;
		}

		public int getCurPage() {
			return curPage;
		}

		public void setCurPage(int curPage) {
			this.curPage = curPage;
		}

		@Override
		public String toString() {
			return "PageIndexBean [startPage=" + startPage + ", endPage="
					+ endPage + ", curPage=" + curPage + "]";
		}
	}

	public static void main(String[] args) {
		for (int curPage = 1; curPage < 20; curPage++) {
			System.out.println(calcPageCount(curPage, 5));
		}

	}
}
